<?php
namespace app\model;

use app\model\config\ConfigError;
use app\model\table\log\OperateLog;
use think\facade\Db;
use Yii;

/**
 *  自定义的异常处理
 */
class MyException extends \Exception
{
    protected $code;
    protected $message;
    ####  这里定义通用错误 ####
    public function __construct($name,$code='-1')
    {
        parent::__construct($name);
        $this->code = $code;
        $this->message = $name;
    }

    public function setCode($code){
        $this->code = $code;
    }

    public function toJson()
    {
        Db::rollback(); // 为了防止事务回滚，日志丢失，所有的错误日志当做必须回滚事务
        header('Content-type:application/json; charset=utf-8');
        OperateLog::traceLog("异常", $this->message);
        echo json_encode(
            array(
                'code' => $this->code,
                'message' => $this->message,
                'data' => "",
            ),JSON_UNESCAPED_UNICODE
        );
        exit;
    }

}
